package swe.user;

import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import java.util.ArrayList;
import java.util.List;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

import swe.article.ArticleBid;


public class User implements Serializable {

	
	private static final long serialVersionUID = 1L;
	
	static int counter = 0;
	private String name;
	private String emailAdd;
	private String userName;
	private byte[] encryptedPassword;
	private byte[] salt;
	private int iD;
	
	/*
	private List<Rating> rating=new ArrayList<Rating>();
	
	public List<Rating> getRating() {
		return rating;
	}
	*/


	public byte[] getSalt() {
		return salt;
	}

	public void setSalt(byte[] salt) {
		this.salt = salt;
	}
	

	/*
	public double getAverageRating() {
		double avg=0;
		int count = 0;
		for(int i=0; i<rating.size();i++) {
			avg = avg+rating.get(i).getRating();
			count++;
		}
		avg=avg/count;
		return avg;
	}
	*/

	/*
	public void setRating(List<Rating> rating) {
		this.rating = rating;
	}
	*/

	public User() {
	}
	
	public User(String name, String emailAdd, String userName, String password) {
		this.name = name;
		this.emailAdd = emailAdd;
		this.userName = userName;
		byte[] salt;
		try {
			salt = generateSalt();
			this.salt = salt;
			this.encryptedPassword = getEncryptedPassword(password, salt); 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.iD = counter++;
	}
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmailAdd() {
		return emailAdd;
	}

	public void setEmailAdd(String emailAdd) {
		this.emailAdd = emailAdd;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getiD() {
		return iD;
	}

	public void setiD(int iD) {
		this.iD = iD;
	}


//	public void addRating(User fromUser, double rating) {
//		Rating rate = new Rating(rating, fromUser);
//		this.rating.add(rate);
//		}
	

	 public byte[] generateSalt() throws NoSuchAlgorithmException {
		  SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
		  byte[] salt = new byte[8];
		  random.nextBytes(salt);
		  
		  return salt;
		 }
	 public byte[] getEncryptedPassword(String password, byte[] salt) throws Exception {
		  String algorithm = "PBKDF2WithHmacSHA1";
		  int derivedKeyLength = 160;
		  int iterations = 10000;
		  KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength);
		  SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);
		  
		  return f.generateSecret(spec).getEncoded();
	 }
	 
	 public byte[] getSavedPassword() {
			return encryptedPassword;
		}
	 

	
//	public Double getAverageRating(){
//		
//		
//		
//		return 1.2;
//	}
	
	
}
